<?php
/**
 * 路由
 * @author 41412754@qq.com
 */
class router {
	
	public function __construct()
	{
		$this->app = &$GLOBALS['app'];
		$this->uri = $this->uri();
	}
	
	/**
	 * 路由
	 */
	public function parseRequest()
	{
		$moduleName = $_REQUEST['mod'];
		$actionName = $_REQUEST['act'];
		if ( ! $moduleName ) $moduleName = $this->app->config->defaultModule;
		if ( ! $actionName ) $actionName = $this->app->config->defaultAction;
		
		$this->app->module = $moduleName;
		$this->app->action = $actionName;
	}
	
	/**
	 * 生成链接
	 * @param $moduleName
	 * @param $methodName
	 * @param $params
	 */
	public function link( $params = null )
	{
		$link['mod'] = $this->app->module;
		$link['act'] = $this->app->action;
		
		if ( $params !== null )
		{
			parse_str( $params, $param );
			$link = array_merge( $link, $param );
		}
		
		foreach ( $link as $key => $val )
			$uri[] = "$key=$val";
		
		if ( $this->app->config->showgroup == true ) return "/{$this->app->group}/?" . implode( '&', $uri );
		else return "/?" . implode( '&', $uri );
	}
	
	/**
	 * 获得当前链接
	 */
	public function uri()
	{
		return $_SERVER['REQUEST_URI'];
	}
}
